<template>
	<xn-form-container
		:title="formData.id ? '编辑结算账户' : '增加结算账户'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="账户名称：" name="accountName">
				<a-input v-model:value="formData.accountName" placeholder="请输入账户名称" allow-clear/>
			</a-form-item>
			<a-form-item label="账号：" name="accountCode">
				<a-input v-model:value="formData.accountCode" placeholder="请输入账号" allow-clear/>
			</a-form-item>
			<a-form-item label="开户行：" name="accountBank">
				<a-input v-model:value="formData.accountBank" placeholder="请输入开户行" allow-clear/>
			</a-form-item>
			<a-form-item label="开户人：" name="accountHolder">
				<a-input v-model:value="formData.accountHolder" placeholder="请输入开户人" allow-clear/>
			</a-form-item>
			<a-form-item label="账户状态：" name="accountStatus">
				<a-radio-group v-model:value="formData.accountStatus" placeholder="请选择账户状态"
							   :options="accountStatusOptions"/>
			</a-form-item>
			<a-form-item label="所属门店：" name="accountStore">
				<xn-page-select
					ref="xnaccountStorePageSelectRef"
					v-model:value="formData.accountStore"
					placeholder="请选择所属门店"
					allow-clear
					show-search
					:page-function="accountStoreSelector"
					:echo-function="accountStoreEcho"
				/>
			</a-form-item>
			<a-form-item label="账户类型：" name="accountAccountType">
				<xn-page-select
					ref="xnaccountAccountTypePageSelectRef"
					v-model:value="formData.accountAccountType"
					placeholder="请选择账户类型"
					allow-clear
					show-search
					:page-function="accountAccountTypeSelector"
					:echo-function="accountAccountTypeEcho"
				/>
			</a-form-item>
			<a-form-item label="账户余额：" name="accountBalance">
				<a-input v-model:value="formData.accountBalance" placeholder="请输入备注" allow-clear/>
			</a-form-item>
			<a-form-item label="备注：" name="accountRemark">
				<a-input v-model:value="formData.accountRemark" placeholder="请输入备注" allow-clear/>
			</a-form-item>
			<a-form-item label="排序码：" name="sortCode">
				<a-input v-model:value="formData.sortCode" placeholder="请输入排序码" allow-clear/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="supplierAccountForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import supplierAccountApi from '@/api/supplier/finance/supplierAccountApi'
import supplierAccountTypeApi from "@/api/supplier/finance/supplierAccountTypeApi";
import supplierStoresApi from "@/api/supplier/base/supplierStoresApi";
// 抽屉状态
const open = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const accountStatusOptions = ref([])
const xnaccountStorePageSelectRef = ref();
const accountStoreSelector = (param) => {
	return supplierStoresApi.supplierStoresPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.storeName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
const accountStoreEcho = (param) => {
	let echoList = []
	let echo =  {}
	for(var i=0;i<param.idList[0].length;i++){
		echo.name = formData.value.accountStoreName
		echo.id = param.idList[0][i]
		echoList.push(echo)
	}
	return Promise.resolve(echoList)
}
const xnaccountAccountTypePageSelectRef = ref();
const accountAccountTypeSelector = (param) => {
	return supplierAccountTypeApi.supplierAccountTypePage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.accountType,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
const accountAccountTypeEcho = (param) => {
	let echoList = []
	let echo =  {}
	for(var i=0;i<param.idList[0].length;i++){
		echo.name = formData.value.accountAccountTypeName
		echo.id = param.idList[0][i]
		echoList.push(echo)
	}
	return Promise.resolve(echoList)
}

// 打开抽屉
const onOpen = (record) => {
	open.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
	accountStatusOptions.value = tool.dictList('COMMON_STATUS')
	nextTick(() => {
		xnaccountStorePageSelectRef.value.onPage({current: 1});
	});
	nextTick(() => {
		xnaccountAccountTypePageSelectRef.value.onPage({current: 1});
	});
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	open.value = false
}
// 默认要校验的
const formRules = {
	accountName: [required('请输入账户名称')],
	accountStatus: [required('请输入账户状态')],
	accountStore: [required('请输入所属门店')],
	accountAccountType: [required('请输入账户类型')],
	accountBalance: [required('请输入账户余额')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			supplierAccountApi
				.supplierAccountSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
		.catch(() => {
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
